/*
 * Copyright (c) 2002-2004, xRhino, Inc.
 *
 * This library is free software; you can redistribute and/or modify
 * it under the terms of the dual license distributed with this source
 * package (a combined Trolltech QPL/GPL-compatible license)
 *
 * Under this dual license scheme, a separate license is available for use of 
 * this code with a proprietary (closed) system. For more information on the 
 * license, please see the LICENSE file distributed with this source package.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SugTransform.h"

/*
  void SugTransform::setMatrix(SugMatrix& matrix)
  {
  //READ ME: Fill this in when you figure out transformations
  }
*/

void SugTransform::setTransform(const char* transformString)
{
   char* tok;
   char* tStr;
   char* typeStr;
   char* valuesStr;

   int valueIndex;
   SugRealType values[6];

   tStr = new char[strlen(transformString) + 1];
   strcpy(tStr, transformString);

   typeStr = tStr;
   valuesStr = strstr(tStr, "(");
   *valuesStr = '\0';
   valuesStr++;
   valuesStr[strlen(valuesStr) - 1] = '\0';

   valueIndex = 0;
   tok = strtok_r(valuesStr, ", ", &valuesStr);
   while (tok)
   {
      values[valueIndex] = atof(tok);
      valueIndex++;
      
      tok = strtok_r(NULL, ", ", &valuesStr);      
   }

   while (valueIndex < 6)
      values[valueIndex++] = 0.0;
   
   if (strcmp(typeStr, "translate") == 0)
   {
      setTranslate(values[0], values[1]);
   }
   else if (strcmp(typeStr, "scale") == 0)
   {
      setScale(values[0], values[1]);
   }
   else if (strcmp(typeStr, "rotate") == 0)
   {
      setRotate(values[0], values[1], values[2]);
   }
   else if (strcmp(typeStr, "skewx") == 0)
   {
      setSkewX(values[0]);
   }
   else if (strcmp(typeStr, "skewy") == 0)
   {
      setSkewY(values[0]);
   }
   else if (strcmp(typeStr, "matrix") == 0)
   {
   }
   
   delete [] tStr;
}

void SugTransform::setTranslate(SugRealType x, SugRealType y)
{
   mX = x;
   mY = y;
   mType = SugTypes::T_TRANSLATE;
}

void SugTransform::setScale(SugRealType x, SugRealType y)
{
   mX = x;
   mY = y;
   mType = SugTypes::T_SCALE;
}

void SugTransform::setRotate(SugRealType angle, SugRealType x, SugRealType y)
{
   mX = x;
   mY = y;
   mAngle = angle;
   mType = SugTypes::T_ROTATE;
}

void SugTransform::setSkewX(SugRealType angle)
{
   mAngle = angle;
   mType = SugTypes::T_SKEWX;
}

void SugTransform::setSkewY(SugRealType angle)
{
   mAngle = angle;
   mType = SugTypes::T_SKEWY;
}

SugRealType SugTransform::getX()
{
   return mX;
}

SugRealType SugTransform::getY()
{
   return mY;
}

SugRealType SugTransform::getAngle()
{
   return mAngle;
}

SugTypes::TransformType SugTransform::getType()
{
   return mType;
}

SugTransform::SugTransform()
{
   mX = mY = mAngle = 0.0;
   mType = SugTypes::T_UNKNOWN;
}

SugTransform::~SugTransform()
{
}

